﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.Security;
using System.Globalization;
using Entetities;
using BLL;

public partial class CreateUser : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {
        CreateUserWizard1.CreatedUser += new EventHandler(CreateUserWizard1_CreatedUser);
        if (!IsPostBack)
        {
            DropDownList drngay = (DropDownList)CreateUserWizard1.CreateUserStep.ContentTemplateContainer.FindControl("drngay");
            DropDownList drthang = (DropDownList)CreateUserWizard1.CreateUserStep.ContentTemplateContainer.FindControl("drthang");
            DropDownList drnam = (DropDownList)CreateUserWizard1.CreateUserStep.ContentTemplateContainer.FindControl("drnam");
            string[] ngay = { "dd", "1", "2", "3", "4", "5", "6", "7", "8", "9", "10", "11", "12", "13", "14", "15", "16", "17",
                          "18", "19", "20", "21", "22", "23", "24", "25", "26", "27", "28", "29", "30", "31" };
            drngay.DataSource = ngay;
            drngay.DataBind();
            drngay.Items[0].Value = "dd";

            string[] thang = { "mm", "1", "2", "3", "4", "5", "6", "7", "8", "9", "10", "11", "12" };
            drthang.DataSource = thang;
            drthang.DataBind();
            drthang.Items[0].Value = "mm";

            drnam.Items.Add("yy");
            drnam.Items[0].Value = "yy";
            for (int i = 2005; i > 1931; i--)
            {
                drnam.Items.Add(i.ToString());
            }

            DropDownList drquocgia = (DropDownList)CreateUserWizard1.CreateUserStep.ContentTemplateContainer.FindControl("drquocgia");
            Dictionary<string, string> objDic = new Dictionary<string, string>();
            foreach (CultureInfo ObjCultureInfo in CultureInfo.GetCultures(CultureTypes.SpecificCultures))
            {
                RegionInfo objRegionInfo = new RegionInfo(ObjCultureInfo.Name);
                if (!objDic.ContainsKey(objRegionInfo.EnglishName))
                {
                    objDic.Add(objRegionInfo.EnglishName, objRegionInfo.TwoLetterISORegionName.ToLower());
                }
            }
            var obj = objDic.OrderBy(p => p.Key);
            drquocgia.Items.Add("----- Chọn quốc gia -----");
            drquocgia.Items[0].Value = "----- Chọn quốc gia -----";
            foreach (KeyValuePair<string, string> val in obj)
            {
                drquocgia.Items.Add(new ListItem(val.Key, val.Value));
            }
        }
    }

    void CreateUserWizard1_CreatedUser(object sender, EventArgs e)
    {
        KhachHangBO khBLL = new KhachHangBO();
        DiaChiBO diachiBLL = new DiaChiBO();
        KhachHangDiaChiBO khdcbll = new KhachHangDiaChiBO();

        TextBox UserName = (TextBox)CreateUserWizard1.CreateUserStep.ContentTemplateContainer.FindControl("UserName");
        MembershipUser currentUser = Membership.GetUser(UserName.Text);
        Guid currentUserId = (Guid)currentUser.ProviderUserKey;
        TextBox txthokh = (TextBox)CreateUserWizard1.CreateUserStep.ContentTemplateContainer.FindControl("txthokh");
        TextBox txttenlot = (TextBox)CreateUserWizard1.CreateUserStep.ContentTemplateContainer.FindControl("txttenlot");
        TextBox txttenkh = (TextBox)CreateUserWizard1.CreateUserStep.ContentTemplateContainer.FindControl("txttenkh");
        TextBox txttencty = (TextBox)CreateUserWizard1.CreateUserStep.ContentTemplateContainer.FindControl("txttencongty");
        DropDownList drngay = (DropDownList)CreateUserWizard1.CreateUserStep.ContentTemplateContainer.FindControl("drngay");
        DropDownList drthang = (DropDownList)CreateUserWizard1.CreateUserStep.ContentTemplateContainer.FindControl("drthang");
        DropDownList drnam = (DropDownList)CreateUserWizard1.CreateUserStep.ContentTemplateContainer.FindControl("drnam");
        TextBox txtdienthoai = (TextBox)CreateUserWizard1.CreateUserStep.ContentTemplateContainer.FindControl("txtdienthoai");
        TextBox txtEmail = (TextBox)CreateUserWizard1.CreateUserStep.ContentTemplateContainer.FindControl("Email");
        TextBox txtdiachi = (TextBox)CreateUserWizard1.CreateUserStep.ContentTemplateContainer.FindControl("txtdiachi");
        DropDownList drquocgia = (DropDownList)CreateUserWizard1.CreateUserStep.ContentTemplateContainer.FindControl("drquocgia");
        TextBox txttinhthanh = (TextBox)CreateUserWizard1.CreateUserStep.ContentTemplateContainer.FindControl("txttinhthanh");
        TextBox txtquanhuyen = (TextBox)CreateUserWizard1.CreateUserStep.ContentTemplateContainer.FindControl("txtquanhuyen");

        int nam, thang, ngay;
        try
        {
            nam = int.Parse(drnam.Text);
            thang = int.Parse(drthang.Text);
            ngay = int.Parse(drngay.Text);
        }
        catch (Exception)
        {
            throw;
        }

        DateTime ngaysinh = new DateTime(nam, thang, ngay);
        KhachHang kh = new KhachHang();
        kh.KhachhangId = currentUserId;
        kh.HoKhachhang = txthokh.Text;
        kh.Tenlotkhachhang = txttenlot.Text;
        kh.Tenkhachhang = txttenkh.Text;
        kh.Ngaysinh = ngaysinh;
        kh.Sodienthoai = txtdienthoai.Text;
        kh.Diachiemail = txtEmail.Text;
        kh.Tencongty = txttencty.Text;
        kh.Diemthuong = 0;

        DiaChi dc = new DiaChi(txtdiachi.Text, txtquanhuyen.Text, txttinhthanh.Text, drquocgia.Text);
        dc.Madiachi = diachiBLL.InsertDiaChi(dc);
        khBLL.InsertKhachHang(kh);

        khdcbll.add_KhachHangDiaChi(kh, dc, 0);
    }
}